/******************************************************************************
** 
**	File name: utest_20.c
**	
**	Description:	RTOS Ver.2 Unit Tests. Event Services.
**
**					This unit test is to verify basic RTOS_WaitEvent() 
**					and RTOS_SetEvent() functionality.
**
**  RTOS API Services to be checked:
**
**
**  Notes:			Should be endless if no errors. Otherwise stops at 
**					RTOS_Halt(). Check breakpoint settings before run!
**
******************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "rtos.h"
#include "config.h"

/******************************************************************************
**	RTOS DECLARARTION SECTION
**
**  Standard config for RTOS MESSAGE QUEUE Services Unit Tests should look
**	as follows:
**
**	Timer interrupt #1:			DUMMY_TimerHandler (defined by USER)
**	Timer interrupt #2:			USER_TimerHandler (defined by USER)
**
**	Timers:									tmr1
**	Events:									evt1,		evt2,		evt3
**	Messages:								n/a
**	Queues:									n/a
**	Timers:									n/a
**	
**	Three tasks:				tsk0,		tsk1,		tsk2,		tsk3 
**  Three priorities:			0(highest)	1 (high)	2 (middle)	3 (low)
**  Stack size:					8			20			20			20
**
**  Notes:	-
**
******************************************************************************/

RTOS_DECLARE_ISR(SYSTEM_TIMER_VECTOR,DUMMY_TimerHandler);
RTOS_DECLARE_ISR(USER_TIMER_VECTOR,USER_TimerHandler);

RTOS_DECLARE_TASK(tsk0,  8, 0);
RTOS_DECLARE_TASK(tsk1, 20, 1);
RTOS_DECLARE_TASK(tsk2, 20, 2);
RTOS_DECLARE_TASK(tsk3, 20, 3);

RTOS_DECLARE_EVENT(evt1);
RTOS_DECLARE_EVENT(evt2);
RTOS_DECLARE_EVENT(evt3);

extern void UTST_FillRegisters( void );

/******************************************************************************
	UNIT TEST MAIN FUNCTION
******************************************************************************/
void main(void)
{
    RTOS_Initialize(0);

	UTST_HwInit();
	usr_tick_init(1000);		// 1KHz (1,000 mSec) timer

    RTOS_TaskInitExp (tsk0);	// Init but not start ))
    RTOS_TaskStartExp(tsk1);
    RTOS_TaskStartExp(tsk2);
    RTOS_TaskStartExp(tsk3);

	RTOS_StartOS();
	
    while(1);	
}


/******************************************************************************
	UNIT TEST INTERRUPT FUNCTION[s]
******************************************************************************/
/*
** Dummy Timer Handler
*/
RTOS_ISR_FUNC(DUMMY_TimerHandler)
{
}

/*
** User' Timer Handler
*/
RTOS_ISR_FUNC(USER_TimerHandler)
{
	RTOS_SetEventISR(&evt3);
}

/******************************************************************************
	UNIT TEST TASK FUNCTIONs
******************************************************************************/
/*
**
*/
RTOS_TASK_FUNC(tsk0)
{
	while (1)
	{
		RTOS_StackCheck(tsk0);
		RTOS_StackCheck(tsk1);
		RTOS_StackCheck(tsk2);
		RTOS_StackCheck(tsk3);
		RTOS_TaskSuspend(&tsk0PID);
	}
}

/*
** RTOS Task #1
*/
RTOS_TASK_FUNC(tsk1)
{
	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		RTOS_WaitEvent(&evt1);		
	}
}

/*
** RTOS Task #2 
*/
RTOS_TASK_FUNC(tsk2)
{
	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		RTOS_WaitEvent(&evt2);		
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		RTOS_SetEvent(&evt1);
	}
}

/*
** RTOS Task #3 
*/
RTOS_TASK_FUNC(tsk3)
{
	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		RTOS_WaitEvent(&evt3);
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		RTOS_SetEvent(&evt2);
	}
}
